﻿var Analysis = {
    ProfileFlag: false,
    VideoFlag: false
};

//投屏
function Video() {
    if (!Analysis.VideoFlag) {
        ShowVideo();
        Analysis.VideoFlag = true;
    } else {
        Analysis.VideoFlag = false;
        // 取消时钟同步
        // if (Cesium.defined(synchronizer)) {
        //     synchronizer = synchronizer.destroy();
        //     document.getElementById('trailer').playbackRate = 1.0;
        // }
        $("#trailer").remove();
        removeAllEntities();
    }

    function ShowVideo() {
        var video = document.createElement("video");
        video.setAttribute("id", "trailer");
        video.setAttribute("autoplay", "true");
        video.setAttribute("loop", "true");
        video.setAttribute("crossorigin", "anonymous");
        // video.setAttribute("crossorigin", "true");
        video.setAttribute("controls", "true");
        video.setAttribute("style", "display: none;-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);-ms-transform: rotate(45deg);-o-transform: rotate(45deg);transform: rotate(45deg);");
        var source = document.createElement("source");
        source.setAttribute("src", "https://v-cdn.zjol.com.cn/280443.mp4");
        // source.setAttribute("src", "http://192.168.8.130:8080/rtsp/live?rtspurl=siwei")
        source.setAttribute("type", "video/mp4");
        // source.setAttribute("src", "http://192.168.8.130:20000/hls/test.m3u8");
        // source.setAttribute("type", "application/x-mpegURL");
        video.appendChild(source);
        document.getElementById("mainBody").appendChild(video);

        var videoElement = document.getElementById('trailer');
        var video = viewer.entities.add({
            polygon: {
                //平面
                hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray([118.688, 39.819, 118.69, 39.799, 118.733, 39.798, 118.733, 39.816])),
                classificationType: Cesium.ClassificationType.BOTH,
                clampToGround: true,
                material: videoElement
            }
        });

        // var videoElement = document.getElementById('trailer');
        // video.polygon.material = videoElement;

        //viewer.zoomTo(video);

        // var video2 = viewer.entities.add({
        //     polygon: {
        //         //高程
        //         hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights([110.46497899144262, 19.95056028548353, 10, 110.46501271535963, 19.950663582294432, 16, 110.46527837363945, 19.95054348658643, 16, 110.46523870433255, 19.95043612289212, 10])),
        //         classificationType: Cesium.ClassificationType.TERRAIN,
        //         clampToGround: false,
        //         material: videoElement,
        //         perPositionHeight: true
        //     }
        // });
        // // 设置与时钟同步
        // synchronizer = new Cesium.VideoSynchronizer({
        //     clock: viewer.clock,
        //     element: videoElement
        // });
    }
}

//笛卡尔坐标系转WGS84坐标系
function Cartesian3_to_WGS84(point) {
    var cartesian33 = new Cesium.Cartesian3(point.x, point.y, point.z);
    var cartographic = Cesium.Cartographic.fromCartesian(cartesian33);
    var lat = Cesium.Math.toDegrees(cartographic.latitude);
    var lng = Cesium.Math.toDegrees(cartographic.longitude);
    var alt = cartographic.height;
    //return { lat: lat, lng: lng, alt: alt };
    return [lng, lat, alt];
}


//转经纬度
function Cartesian3_to_WGS84Arr(points) {
    var thisResult = [];
    for (var i = 0; i < points.length; i++) {
        thisResult.push(Cartesian3_to_WGS84(points[i]));
    }
    return thisResult;
}

//计算直线坐标点数组距离
function getPointsDistance(pointArr, isWGS84) {
    var distance = 0;
    var distanceOne = 0;
    var circlePoint1_WGS84, circlePoint2_WGS84;
    for (var i = 0; i < pointArr.length - 1; i++) {
        if (isWGS84) {
            circlePoint1_WGS84 = pointArr[i];
            circlePoint2_WGS84 = pointArr[i + 1];
        } else {
            circlePoint1_WGS84 = Cartesian3_to_WGS84(pointArr[i]);
            circlePoint2_WGS84 = Cartesian3_to_WGS84(pointArr[i + 1]);
        }
        if (circlePoint1_WGS84.toString() != circlePoint2_WGS84.toString()) {
            distanceOne = getFlatternDistance(circlePoint1_WGS84[0], circlePoint1_WGS84[1], circlePoint2_WGS84[0], circlePoint2_WGS84[1]);
        }
        distance += distanceOne;
    }
    return distance;
}

//计算两点间距离
function getFlatternDistance(lng1, lat1, lng2, lat2) {
    var EARTH_RADIUS = 6378137.0; //单位M
    var PI = Math.PI;

    function getRad(d) {
        return d * PI / 180.0;
    }
    var f = getRad((lat1 + lat2) / 2);
    var g = getRad((lat1 - lat2) / 2);
    var l = getRad((lng1 - lng2) / 2);

    var sg = Math.sin(g);
    var sl = Math.sin(l);
    var sf = Math.sin(f);

    var s, c, w, r, d, h1, h2;
    var a = EARTH_RADIUS;
    var fl = 1 / 298.257;

    sg = sg * sg;
    sl = sl * sl;
    sf = sf * sf;

    s = sg * (1 - sl) + (1 - sf) * sl;
    c = (1 - sg) * (1 - sl) + sf * sl;

    w = Math.atan(Math.sqrt(s / c));
    r = Math.sqrt(s * c) / w;
    d = 2 * w * a;
    h1 = (3 * r - 1) / 2 / c;
    h2 = (3 * r + 1) / 2 / s;

    return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
}